/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package javax.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;



/**
 * ServletRequestWrapper提供了一个简便实现类，在对ServletRequest有改动需求的时，可以重写此类，之后Request的操作都会经过重写的类.
 *
 *
 * 实际应用场景中，有时可能会有这样的需求：需要在请求进入应用之前，对请求进行统一处理，增加或减少到达应用的数据量，或者变换应用使用的HttpServletRequest的API，
 * 这时就可以创建一个类继承HttpServletRequestWrapper，它可以对HttpServletRequest进行再封装，然后之后的请求都会经过这个类，应用接收到的请求对象也会是这个类。
 *
 * HttpServletRequestWrapper集成了ServletRequestWrapper，ServletRequestWrapper是对ServletRequest的包装，
 * HttpServletRequestWrapper是对HttpServletRequest的包装，其实查看源码后会发现，ServletRequestWrapper和HttpServletRequestWrapper
 * 提供的API完全调用的ServletRequest和HttpServletRequest的API进行实现。在我们进行应用时，使用HttpServletRequestWrapper即可，
 * 它同时拥有ServletRequestWrapper和HttpServletRequestWrapper的功能。

 *
 * Provides a convenient implementation of the ServletRequest interface that
 * can be subclassed by developers wishing to adapt the request to a Servlet.
 * This class implements the Wrapper or Decorator pattern. Methods default to
 * calling through to the wrapped request object.
  * @since	v 2.3
 * 
 * 
 *
 * @see 	ServletRequest
 *
 */

public class ServletRequestWrapper implements ServletRequest {
    /**
     * ServletRequestWrapper持有ServletRequest的实例.
     */
    private ServletRequest request;

	/**
	 * 构建ServletRequestWrapper，传入ServletRequest的值.
     * Creates a ServletRequest adaptor wrapping the given request object.
	 * @throws IllegalArgumentException if the request is null
	 */

    public ServletRequestWrapper(ServletRequest request) {
	if (request == null) {
	    throw new IllegalArgumentException("Request cannot be null");   
	}
	this.request = request;
    }

	/**
	 * 获取ServletRequest实例.
     * Return the wrapped request object.
	 */
	public ServletRequest getRequest() {
		return this.request;
	}
	
	/**
	 * 设置ServletRequest实例.
     *
     * Sets the request object being wrapped.
	 * @throws IllegalArgumentException if the request is null.
	 */
	
	public void setRequest(ServletRequest request) {
	    if (request == null) {
		throw new IllegalArgumentException("Request cannot be null");
	    }
	    this.request = request;
	}

    /**
     * 获取Servlet的属性.
     *
     * The default behavior of this method is to call getAttribute(String name)
     * on the wrapped request object.
     */

    public Object getAttribute(String name) {
	return this.request.getAttribute(name);
	}
    
    

    /**
     * 获取所有属性枚举格式.
     *
     * The default behavior of this method is to return getAttributeNames()
     * on the wrapped request object.
     */

    public Enumeration getAttributeNames() {
	return this.request.getAttributeNames();
	}    
    
    
    
    /**
     * 返回请求中输入内容的字符编码类型，如果没有定义字符编码类型就返回空值.
     *
     * The default behavior of this method is to return getCharacterEncoding()
     * on the wrapped request object.
     */

    public String getCharacterEncoding() {
	return this.request.getCharacterEncoding();
	}
	
    /**
     * 设置输入内容的字符编码类型.
     *
     * The default behavior of this method is to set the character encoding
     * on the wrapped request object.
     */

    public void setCharacterEncoding(String enc) throws java.io.UnsupportedEncodingException {
	this.request.setCharacterEncoding(enc);
	}
    
    
    /**
     * 请求内容的长度，如果长度未知就返回-1.
     *
     * The default behavior of this method is to return getContentLength()
     * on the wrapped request object.
     */

    public int getContentLength() {
	return this.request.getContentLength();
    }
    
    
    

    /**
     * 返回请求数据体的MIME类型CONTENT-TYPE，如果类型未知返回空值.
     *
     * The default behavior of this method is to return getContentType()
     * on the wrapped request object.
     */
    public String getContentType() {
	return this.request.getContentType();
    }
    
    
    

     /**
      * 返回一个输入流，使用该输入流以二进制方式读取请求正文的内容.
      *      javax.servlet.ServletInputStream是一个抽象类，继承自InputStream.
      *
      * The default behavior of this method is to return getInputStream()
      * on the wrapped request object.
      */

    public ServletInputStream getInputStream() throws IOException {
	return this.request.getInputStream();
	}
     
    
    

    /**
     * 根据指定参数名获取参数值.
     *
     * The default behavior of this method is to return getParameter(String name)
     * on the wrapped request object.
     */

    public String getParameter(String name) {
	return this.request.getParameter(name);
    }
    
    /**
     * 获取参数的Map形式，包括所有参数.
     *
     * The default behavior of this method is to return getParameterMap()
     * on the wrapped request object.
     */
    public Map getParameterMap() {
	return this.request.getParameterMap();
    }
    
    
    

    /**
     * 获取所有参数名的枚举.
     *
     * The default behavior of this method is to return getParameterNames()
     * on the wrapped request object.
     */
     
    public Enumeration getParameterNames() {
	return this.request.getParameterNames();
    }
    
    
    

    /**
     * 根据指定属性名获取参数值数组.
     *
     * The default behavior of this method is to return getParameterValues(String name)
     * on the wrapped request object.
     */
    public String[] getParameterValues(String name) {
	return this.request.getParameterValues(name);
	}
    
    
    

     /**
      * 返回请求使用的协议信息.格式为：协议/主版本号.次版本号.例如：http/1.0.
      *
      * The default behavior of this method is to return getProtocol()
      * on the wrapped request object.
      */
    
    public String getProtocol() {
	return this.request.getProtocol();
	}
    
    
    

    /**
     * 返回请求所使用的URL的模式.若http、https等.
     *
     * The default behavior of this method is to return getScheme()
     * on the wrapped request object.
     */
    

    public String getScheme() {
	return this.request.getScheme();
	}
    
    
    

    /**
     * 返回请求发送到的服务器的主机名.
     *
     * The default behavior of this method is to return getServerName()
     * on the wrapped request object.
     */
    public String getServerName() {
	return this.request.getServerName();
	}
    
    
    

   /**
    * 返回请求发送到的服务器的端口号.
    *
    * The default behavior of this method is to return getServerPort()
    * on the wrapped request object.
    */

    public int getServerPort() {
	return this.request.getServerPort();
	}
    
    
    
   /**
    * 返回BufferedReader对象，以字节数据方式读取请求正文.
    *
    * The default behavior of this method is to return getReader()
    * on the wrapped request object.
    */

    public BufferedReader getReader() throws IOException {
	return this.request.getReader();
	}
    
    
    

    /**
     * 返回发送请求的客户端或最后一个代理服务器的IP地址.
     *
     * The default behavior of this method is to return getRemoteAddr()
     * on the wrapped request object.
     */
    
    public String getRemoteAddr() {
	return this.request.getRemoteAddr();
    }
    
    
    

    /**
     * 返回发送请求的客户端或最后一个代理服务器的主机名.
     *
     * The default behavior of this method is to return getRemoteHost()
     * on the wrapped request object.
     */

    public String getRemoteHost() {
	return this.request.getRemoteHost();
    }
    
    
    

    /**
     * 根据传递的属性名和属性值设置Request属性.
     *
     * The default behavior of this method is to return setAttribute(String name, Object o)
     * on the wrapped request object.
     */

    public void setAttribute(String name, Object o) {
	this.request.setAttribute(name, o);
    }
    
    
    

    /**
     * 从Request中删除指定的属性名对应的值.一般使用此方法.
     *
     * The default behavior of this method is to call removeAttribute(String name)
     * on the wrapped request object.
     */
    public void removeAttribute(String name) {
	this.request.removeAttribute(name);
    }
    
    
    

    /**
     * 根据客户端传递的Accept-Language对应的区域设置.
     *      若客户端未指定Accept-Language，则返回服务器默认语言环境.
     *
     * The default behavior of this method is to return getLocale()
     * on the wrapped request object.
     */

    public Locale getLocale() {
	return this.request.getLocale();
    }
    
    
    

    /**
     * 返回Locale对象的枚举，从首选区域开始按降序返回基于Accept-Language头的客户端可接受的区域.
     *       如果客户机请求不提供Accept-Language头，此方法返回包含一个Locale的枚举，即是服务器默认语言环境对应的Locale.
     *
     *
     * The default behavior of this method is to return getLocales()
     * on the wrapped request object.
     */

    public Enumeration getLocales() {
	return this.request.getLocales();
    }
    
    
    

    /**
     * 指示是否使用安全通道（如HTTPS）发出此请求.
     *
     * The default behavior of this method is to return isSecure()
     * on the wrapped request object.
     */

    public boolean isSecure() {
	return this.request.isSecure();
    }
    
    
    

    /**
     * 返回RequestDispatcher对象，作为path所定位的资源的封装.
     *       RequestDispatcher用于服务器请求转发.
     *
     * The default behavior of this method is to return getRequestDispatcher(String path)
     * on the wrapped request object.
     */

    public RequestDispatcher getRequestDispatcher(String path) {
	return this.request.getRequestDispatcher(path);
    }
    
    
    

    /**
     * Servlet API 2.1开始已不推荐使用此API.
     *      取得文件在服务器上的绝对路径.
     *
     * The default behavior of this method is to return getRealPath(String path)
     * on the wrapped request object.
     */

    public String getRealPath(String path) {
	return this.request.getRealPath(path);
    }
    
    /**
     * 返回发送请求的客户端或者最后一个代理服务器的IP源端口， 这个方法是在Servlet 2.4规范中新增的方法.
     *
     * The default behavior of this method is to return
     * getRemotePort() on the wrapped request object.
     *
     * @since 2.4
     */    
    public int getRemotePort(){
        return this.request.getRemotePort();
    }


    /**
     * 返回接收到请求的IP接口的主机名，这个方法是在Servlet 2.4规范中新增的方法.
     *
     * The default behavior of this method is to return
     * getLocalName() on the wrapped request object.
     *
     * @since 2.4
     */
    public String getLocalName(){
        return this.request.getLocalName();
    }

    /**
     * 返回接收到请求的网络接口的IP地址，这个方法是在Servlet 2.4规范中新增的方法.
     *
     * The default behavior of this method is to return
     * getLocalAddr() on the wrapped request object.
     *
     * @since 2.4
     */       
    public String getLocalAddr(){
        return this.request.getLocalAddr();
    }


    /**
     * 返回接收到请求的网络接口的IP端口号，这个方法是在Servlet 2.4规范中新增的方法.
     *
     * The default behavior of this method is to return
     * getLocalPort() on the wrapped request object.
     *
     * @since 2.4
     */
    public int getLocalPort(){
        return this.request.getLocalPort();
    }
    
}

